package com.scaffold.authority.service;

import java.sql.Timestamp;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import wangjg.commons.page.Page;

import com.scaffold.authority.dao.AuthorityDAO;
import com.scaffold.authority.dao.ResourceDAO;
import com.scaffold.authority.dao.ResourceAuthorityRelationDAO;
import com.scaffold.authority.entity.Authority;
import com.scaffold.authority.entity.Resource;
import com.scaffold.authority.entity.ResourceAuthorityRelation;

@Service
@Transactional
public class AuthorityService{
	@Autowired
	private AuthorityDAO authorityDAO;
	@Autowired
	private ResourceDAO resourceDAO;
	@Autowired
	private ResourceAuthorityRelationDAO resourceAuthorityRelationDAO;

	public Authority getAuthority(long id ) {
		return this.authorityDAO.findById(id);
	}

	public void save(Authority authority){
		Timestamp now = new Timestamp(System.currentTimeMillis());
		if(authority.getId()==null){
			authority.setCreated(now);
			authority.setModified(now);
			this.authorityDAO.save(authority);
		}else{
			authority.setModified(now);
			this.authorityDAO.update(authority);
		}
	}

	public void update(Authority authority){
		Timestamp now = new Timestamp(System.currentTimeMillis());
		authority.setModified(now);
		this.authorityDAO.update(authority);
	}

	public void delete(Authority authority){
		this.resourceAuthorityRelationDAO.deleteByProperty("authorityId", authority.getId());
		this.authorityDAO.delete(authority);
	}

	public void batchDelete(String idseq) {
		String[] sa = idseq.split(",");
		for(String s:sa){
			Long id = Long.parseLong(s);
			this.resourceAuthorityRelationDAO.deleteByProperty("authorityId", id);
			this.authorityDAO.delete(id);
		}
	}

	public List<Authority> pageQuery(Page page , Integer type, String name, String displayName, String keyword) {
		return this.authorityDAO.pageQuery(page , type, name, displayName, keyword);
	}


	public void saveResources(Long authorityId, String resourceIds) {
		resourceAuthorityRelationDAO.deleteByProperty("authorityId", authorityId);
		
		if(authorityId==null || resourceIds==null || resourceIds.isEmpty()){
			return;
		}
		String[] idseq = resourceIds.split(",");
		Timestamp now = new Timestamp(System.currentTimeMillis());
		for(String s:idseq){
			Long resourceId = Long.parseLong(s);
			ResourceAuthorityRelation item = new ResourceAuthorityRelation();
			item.setAuthorityId(authorityId);
			item.setResourceId(resourceId);
			item.setCreated(now);
			item.setModified(now);
			resourceAuthorityRelationDAO.save(item);
		}
	}

	public List<Long> getResourceIdList(Long authorityId) {
		return resourceAuthorityRelationDAO.findPropertyList("resourceId", "authorityId", authorityId);
	}

	public List<Resource> getResourceList(Long authorityId) {
		List idList =  resourceAuthorityRelationDAO.findPropertyList("resourceId", "authorityId", authorityId);
		return resourceDAO.findByIdList(idList);
	}
}
